﻿using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace TestConsoleReport
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
            Warning[] warnings;
            string[] streamids;
            string mimeType, encoding, extension;
            string filenameToSave = "test" + DateTime.Now.ToString("yyyyMMdd_HHmmss")+  ".PDF";

            LocalReport report = new LocalReport();
            /*
             define here everything about your report: rdlc file, parameters, options etc.
             * 
            */
            using (System.IO.StreamReader r = new StreamReader("ClaimReport.rdlc"))
            {
                report.LoadReportDefinition(r);
            }

            

            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data Source=KLDB3\\SQL2008;Initial Catalog=CDMS_UAT;Persist Security Info=True;User ID=CDMS_UAT_user;Password=Xh57887266;MultipleActiveResultSets=True;Application Name=e-Overtime;Workstation ID=AN2594"))
            {

                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM Claim;";

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
                da.Fill(ds, "Claim_DataSet");
            }

            ReportDataSource rds = new ReportDataSource("Claim_DataSet", ds.Tables["Claim_DataSet"]);
            report.DataSources.Add(rds);



            FileStream newFile = new FileStream(filenameToSave, FileMode.Create);

            //string renderFormat = (filenameToSave.EndsWith(".xlsx") ? "EXCELOPENXML" : "Excel");

            byte[] bytes = report.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
            newFile.Write(bytes, 0, bytes.Length);
            newFile.Close();
        }
    }
}
